package tr.com.argela.tap.mot.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import tr.com.argela.tap.mot.logger.LoggerFactory;
import tr.com.argela.tap.mot.models.Project;
import tr.com.argela.tap.mot.models.User;

public class ProjectDao {

    private static Logger logger = LoggerFactory.getLogger("servlets");

    public ProjectDao() {
    }

    public int addProject(DbManager dbManager, String name, User user, String desc) {
        Project project = null;
        PreparedStatement stmt = null;
        int effectedRows = 0;

        String sql = "insert into project (name,user_id,description) values (?,?,?)";
        System.out.println("Query: " + sql);
        try {
            stmt = dbManager.getConn().prepareStatement(sql);
            stmt.setString(1, name);
            stmt.setLong(2, user.getId());
            stmt.setString(3, desc);

            effectedRows = stmt.executeUpdate();

        } catch (SQLException ex) {
            System.out.println("Error: " + ex);
            ex.printStackTrace();
            logger.error("Error", ex);
        } finally {
            if (stmt != null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

        }

        if (project == null) {
            System.out.println("Project is null: ");
        }
        return effectedRows;
    }

    public int updateProject(DbManager dbManager, String name, User user, String desc, Long projectId) {
        Project project = null;
        PreparedStatement stmt = null;
        int effectedRows = 0;

        String sql = "update project set name = ?,user_id = ?, description = ?  where id = ? ";
        System.out.println("Query: " + sql);
        try {
            stmt = dbManager.getConn().prepareStatement(sql);
            stmt.setString(1, name);
            stmt.setLong(2, user.getId());
            stmt.setString(3, desc);
            stmt.setLong(4, projectId);

            effectedRows = stmt.executeUpdate();

        } catch (SQLException ex) {
            System.out.println("Error: " + ex);
            ex.printStackTrace();
            logger.error("Error", ex);
        } finally {
            if (stmt != null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

        }

        if (project == null) {
            System.out.println("Project is null: ");
        }
        return effectedRows;
    }

    public List<Project> seeProjects() {
        Project project = null;
        PreparedStatement stmt = null;
        List<Project> projectList = new ArrayList<Project>();
        String sql = "select * from project ";
        System.out.println("Query: " + sql);
        ResultSet rs;
        DbManager dbManager = null;
        try {
            dbManager = new DbManager();
            stmt = dbManager.getConn().prepareStatement(sql);

            rs = stmt.executeQuery();
            System.out.println("Rs size: " + rs.getRow());
            while (rs.next()) {
                project = new Project();

                project.setId(rs.getLong("id"));
                project.setName(rs.getString("name"));

                User user = new User();
                user.setId(rs.getLong("user_id"));
                project.setUser(user);
                project.setDescription(rs.getString("description"));
                projectList.add(project);

            }

        } catch (SQLException ex) {
            System.out.println("Error: " + ex);
            ex.printStackTrace();
            logger.error("Error", ex);
        } finally {
            if (stmt != null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            dbManager.close();
        }

        if (project == null) {
            System.out.println("Project is null: ");
        }
        return projectList;
    }

    public Project checkproject(Long id) {

        Project project = null;
        PreparedStatement stmt = null;
        String sql = "select * from project where id = ?";
        System.out.println("Query: " + sql);
        ResultSet rs;
        DbManager dbManager = null;
        try {
            dbManager = new DbManager();
            stmt = dbManager.getConn().prepareStatement(sql);
            stmt.setLong(1, id);
            rs = stmt.executeQuery();
            project = new Project();
            while (rs.next()) {
                project.setId(rs.getLong("id"));
                project.setName(rs.getString("name"));

                User user = new User();
                user.setId(rs.getLong("user_id"));
                project.setUser(user);
                project.setDescription(rs.getString("description"));
            }

        } catch (SQLException ex) {
            System.out.println("Error: " + ex);
            ex.printStackTrace();
            logger.error("Error", ex);
        } finally {
            if (stmt != null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            dbManager.close();
        }

        if (project == null) {
            System.out.println("Project is null: ");
        }
        return project;

    }

}